###########################################################
###########################################################
##
##	Beyond Prices: The Drivers of Farmer Loan Default
##
##				Liz Walker and Erin Frey
##
##				 Statistical Code for R
##
###########################################################
###########################################################

mumdata<-read.csv("/Users/ElizabethWalker/Desktop/data/mumdata.csv", header=TRUE)

as.data.frame(mumdata)


###################################################
##	Subset of Farmers Reached for Follow-Up
###################################################
new.mum<-mumdata[which(mumdata$sample=='1'),]

####################################################
##	Subset of Farmers in Control Group
####################################################
new.mum.cont<-new.mum[which(new.mum$treatment=='0'),]

####################################################
##	Subset of Farmers in Treatment Group
####################################################
new.mum.tr<-new.mum[which(new.mum$treatment=='1'),]





##################################################
##################################################
##					PRICES
##################################################
##################################################


################################################
##					Maize Price
##	NOTE: 	This analysis only includes farmers
##			that grew ONLY maize; it includes
##			58 observations, though 4
##			appear to have a price of 0
##			
################################################



nogegg.mum<-new.mum[which(new.mum$gegg=='0'),]
maize.mum<-nogegg.mum[which(nogegg.mum$quantmaizea>'0'),]

maize.mum$pricemaize<-maize.mum$revenuetot/maize.mum$quantmaizec
maize.mum$pricemaize
mean(maize.mum$pricemaize)
sepricemaize<-sd(maize.mum$pricemaize)/sqrt(nrow(maize.mum))

##		Graph of Maize Prices
pdf(file="price.pdf", width = 5, height = 5, family = "Helvetica", pointsize = 10)
plot(density(maize.mum$pricemaize), main="Average Price Recieved by Farmers", xlab="Price, Maize Farmers", ylab="Density", xlim=c(0,1.5))
dev.off()



###########################################
##	Garden Egg Price
###########################################

nomaize.mum<-new.mum[which(new.mum$maize=='0'),]
gegg.mum<-nomaize.mum[which(nomaize.mum$quantgegga>'0'),]

gegg.mum$pricegegg<-gegg.mum$revenuetot/gegg.mum$quantgegga
gegg.mum$pricegegg
mean(gegg.mum$pricegegg)









####################################################
##	Analysis:		The Effect of Revenue, Price,
##					and Quantity on the Default Rate
###################################################

##	Default Rate and Revenue
overdue.maize.rev<-zelig(overdue09may ~ principal0 + dependents + loanbank2 + monfarm + ambiguity + female + revenuetot + soldmarkettrader + preferbank, data=maize.mum, model="logit")
summary(overdue.maize.rev)

## Default Rate and Price
overdue.maize.pr<-zelig(overdue09may ~ pricemaize + principal0 + dependents + loanbank2 + monfarm + ambiguity + female + soldmarkettrader + preferbank, data=maize.mum, model="logit")
summary(overdue.maize.pr)

##	Default Rate and Quantity and Price
overdue.maize.quant.pr<-zelig(overdue09may ~ quantmaizea + pricemaize + principal0 + dependents + loanbank2 + monfarm + ambiguity + female + soldmarkettrader + preferbank + treatment, data=maize.mum, model="logit")
summary(overdue.maize.quant.pr)



#########################################################
##	QUANTITIES OF INTEREST FOR PRICE/QUANTITY ON DEFAULT
#########################################################

##	Price
summary(maize.mum$pricemaize)
overdue.m.lowprice<-setx(overdue.maize.quant.pr, pricemaize=".30")
overdue.m.highprice<-setx(overdue.maize.quant.pr, pricemaize=".71")
fd.price.overdue.maize<-sim(overdue.maize.quant.pr, x=overdue.m.highprice, x1=overdue.m.lowprice)
summary(fd.price.overdue.maize)

##	Quantity
summary(maize.mum$quantmaizea)
overdue.m.lowquant<-setx(overdue.maize.quant.pr, quantmaizea="106.2")
overdue.m.highquant<-setx(overdue.maize.quant.pr, quantmaizea="687.5")
fd.quant.overdue.maize<-sim(overdue.maize.quant.pr, x=overdue.m.highquant, x1=overdue.m.lowquant)
summary(fd.quant.overdue.maize)

##	Principal
summary(maize.mum$principal0)
overdue.m.lowprinc<-setx(overdue.maize.quant.pr, principal0="25")
overdue.m.highprinc<-setx(overdue.maize.quant.pr, principal0="300")
fd.princ.overdue.maize<-sim(overdue.maize.quant.pr, x=overdue.m.highprinc, x1=overdue.m.lowprinc)
summary(fd.princ.overdue.maize)








################################################################
################################################################
##		CLUSTERS
###############################################################
###############################################################


##	Dummy Variables for Villages
new.mum$village.f<-factor(new.mum$village)
maize.mum$village.f<-factor(maize.mum$village)
gegg.mum$village.f<-factor(gegg.mum$village)

##	Dummy Variables for Clusters
new.mum$cluster.f<-factor(new.mum$cluster)
maize.mum$cluster.f<-factor(maize.mum$cluster)
gegg.mum$cluster.f<-factor(gegg.mum$cluster)



###########################################
##	Default Rate by Cluster 
###########################################

clust1.mum <-new.mum[which(new.mum$cluster=='1'),]
c1.default <-mean(na.omit(clust1.mum$overdue09may))
c1.default

clust2.mum <-new.mum[which(new.mum$cluster=='2'),]
c2.default <-mean(na.omit(clust2.mum$overdue09may))
c2.default

clust3.mum <-new.mum[which(new.mum$cluster=='3'),]
c3.default<-mean(na.omit(clust3.mum$overdue09may))
c3.default

clust4.mum <-new.mum[which(new.mum$cluster=='4'),]
c4.default<-mean(na.omit(clust4.mum$overdue09may))
c4.default

clust5.mum <-new.mum[which(new.mum$cluster=='5'),]
c5.default<-mean(na.omit(clust5.mum$overdue09may))
c5.default

clust6.mum <-new.mum[which(new.mum$cluster=='6'),]
c6.default <-mean(na.omit(clust6.mum$overdue09may))
c6.default

clust7.mum <-new.mum[which(new.mum$cluster=='7'),]
c7.default <-mean(na.omit(clust7.mum$overdue09may))
c7.default

clust8.mum <-new.mum[which(new.mum$cluster=='8'),]
c8.default<-mean(na.omit(clust8.mum$overdue09may))
c8.default

clust9.mum <-new.mum[which(new.mum$cluster=='9'),]
c9.default<-mean(na.omit(clust4.mum$overdue09may))
c9.default

clust10.mum <-new.mum[which(new.mum$cluster=='10'),]
c10.default<-mean(na.omit(clust10.mum$overdue09may))
c10.default

clust11.mum <-new.mum[which(new.mum$cluster=='11'),]
c11.default <-mean(na.omit(clust1.mum$overdue09may))
c11.default

clust12.mum <-new.mum[which(new.mum$cluster=='12'),]
c12.default <-mean(na.omit(clust2.mum$overdue09may))
c2.default

clust13.mum <-new.mum[which(new.mum$cluster=='13'),]
c13.default<-mean(na.omit(clust3.mum$overdue09may))
c13.default

clust14.mum <-new.mum[which(new.mum$cluster=='14'),]
c14.default<-mean(na.omit(clust14.mum$overdue09may))
c14.default

clust15.mum <-new.mum[which(new.mum$cluster=='15'),]
c15.default<-mean(na.omit(clust15.mum$overdue09may))
c15.default

clust16.mum <-new.mum[which(new.mum$cluster=='16'),]
c16.default <-mean(na.omit(clust16.mum$overdue09may))
c16.default

clust17.mum <-new.mum[which(new.mum$cluster=='17'),]
c17.default <-mean(na.omit(clust17.mum$overdue09may))
c17.default

clust18.mum <-new.mum[which(new.mum$cluster=='18'),]
c18.default<-mean(na.omit(clust18.mum$overdue09may))
c18.default

clust19.mum <-new.mum[which(new.mum$cluster=='19'),]
c19.default<-mean(na.omit(clust19.mum$overdue09may))
c19.default

xtable(t(cbind(c1.default, c2.default, c3.default, c4.default, c5.default, c6.default, c7.default, c8.default, c9.default, c10.default, c11.default, c12.default, c13.default, c14.default, c15.default, c16.default, c17.default, c18.default, c19.default)))




################################################################
##	Analysis on Default Rate and Clusters
################################################################

##	All Crops with Cluster, no Prices
overdue.cl.f<-zelig(overdue09may ~ revenuetot + principal0 + cluster.f + loanbank2 + preferbank, data=new.mum, model="logit")
summary(overdue.cl.f)
overdue.cl.f$coef


##	FIRST DIFFERENCES

overdue.cl<-zelig(overdue09may ~ revenuetot + principal0 + cluster + loanbank2 + preferbank, data=new.mum, model="logit")
summary(overdue.cl)
overdue.cl$coef

cl.1<-setx(overdue.cl, cluster="1")
cl.2<-setx(overdue.cl, cluster="2")
cl.3<-setx(overdue.cl, cluster="3")
cl.4<-setx(overdue.cl, cluster="4")
cl.5<-setx(overdue.cl, cluster="5")
cl.6<-setx(overdue.cl, cluster="6")
cl.7<-setx(overdue.cl, cluster="7")
cl.8<-setx(overdue.cl, cluster="8")
cl.9<-setx(overdue.cl, cluster="9")
cl.10<-setx(overdue.cl, cluster="10")
cl.11<-setx(overdue.cl, cluster="11")
cl.12<-setx(overdue.cl, cluster="12")
cl.13<-setx(overdue.cl, cluster="13")
cl.14<-setx(overdue.cl, cluster="14")
cl.15<-setx(overdue.cl, cluster="15")
cl.16<-setx(overdue.cl, cluster="16")
cl.17<-setx(overdue.cl, cluster="17")
cl.18<-setx(overdue.cl, cluster="18")
cl.19<-setx(overdue.cl, cluster="19")

fd.cl.2<-sim(overdue.cl, x1 = cl.2, x = cl.1)
summary(fd.cl.2)
fd.cl.3<-sim(overdue.cl, x1=cl.3, x=cl.1)
summary(fd.cl.3)
fd.cl.4<-sim(overdue.cl, x1=cl.4, x=cl.1)
summary(fd.cl.4)
fd.cl.5<-sim(overdue.cl, x1=cl.5, x=cl.1)
summary(fd.cl.5)
fd.cl.6<-sim(overdue.cl, x1=cl.6, x=cl.1)
summary(fd.cl.6)
fd.cl.7<-sim(overdue.cl, x1=cl.7, x=cl.1)
summary(fd.cl.7)
fd.cl.8<-sim(overdue.cl, x1=cl.8, x=cl.1)
summary(fd.cl.8)
fd.cl.9<-sim(overdue.cl, x1=cl.9, x=cl.1)
summary(fd.cl.9)
fd.cl.10<-sim(overdue.cl, x1=cl.10, x=cl.1)
summary(fd.cl.10)
fd.cl.11<-sim(overdue.cl, x1=cl.11, x=cl.1)
summary(fd.cl.11)
fd.cl.12<-sim(overdue.cl, x1=cl.12, x=cl.1)
summary(fd.cl.12)
fd.cl.13<-sim(overdue.cl, x1=cl.13, x=cl.1)
summary(fd.cl.13)
fd.cl.14<-sim(overdue.cl, x1=cl.14, x=cl.1)
summary(fd.cl.14)
fd.cl.15<-sim(overdue.cl, x1=cl.15, x=cl.1)
summary(fd.cl.15)
fd.cl.16<-sim(overdue.cl, x1=cl.16, x=cl.1)
summary(fd.cl.16)
fd.cl.17<-sim(overdue.cl, x1=cl.17, x=cl.1)
summary(fd.cl.17)
fd.cl.18<-sim(overdue.cl, x1=cl.18, x=cl.1)
summary(fd.cl.18)
fd.cl.19<-sim(overdue.cl, x1=cl.19, x=cl.1)
summary(fd.cl.19)


summary(fd.cl.2)
summary(fd.cl.3)
summary(fd.cl.4)
summary(fd.cl.5)
summary(fd.cl.6)
summary(fd.cl.7)
summary(fd.cl.8)
summary(fd.cl.9)
summary(fd.cl.10)
summary(fd.cl.11)
summary(fd.cl.12)
summary(fd.cl.13)
summary(fd.cl.14)
summary(fd.cl.15)
summary(fd.cl.16)
summary(fd.cl.17)
summary(fd.cl.18)
summary(fd.cl.19)





